﻿// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using Xunit;

namespace System.Numerics.Tests
{
    public partial class ExtractBytesMembersTests
    {
        // int => signed little endian byte representation
        // the matrix of unsigned / big-endian is built from this.
        public static IEnumerable<object[]> FromIntTests_MemberDataSeed()
        {
            yield return new object[] { 0, new byte[] { 0x00 } };
            yield return new object[] { 3, new byte[] { 0x03 } };
            yield return new object[] { 128, new byte[] { 0x80, 0x00 } };
            yield return new object[] { 200, new byte[] { 0xc8, 0x00 } };
            yield return new object[] { 256, new byte[] { 0x00, 0x01 } };
            yield return new object[] { 2005, new byte[] { 0xd5, 0x07 } };
            yield return new object[] { 10197, new byte[] { 0xd5, 0x27 } };
            yield return new object[] { 33023, new byte[] { 0xff, 0x80, 0x00 } };
            yield return new object[] { 2368349, new byte[] { 0x5d, 0x23, 0x24 } };
            yield return new object[] { 10756957, new byte[] { 0x5d, 0x23, 0xa4, 0x00 } };
            yield return new object[] { 193100307, new byte[] { 0x13, 0x7a, 0x82, 0x0b } };
            yield return new object[] { 1266842131, new byte[] { 0x13, 0x7a, 0x82, 0x4b } };
            yield return new object[] { int.MaxValue, new byte[] { 0xff, 0xff, 0xff, 0x7f } };
            yield return new object[] { -1, new byte[] { 0xff } };
            yield return new object[] { -128, new byte[] { 0x80 } };
            yield return new object[] { -172, new byte[] { 0x54, 0xff } };
            yield return new object[] { -23439, new byte[] { 0x71, 0xa4 } };
            yield return new object[] { -51301, new byte[] { 0x9b, 0x37, 0xff } };
            yield return new object[] { -126341, new byte[] { 0x7b, 0x12, 0xfe } };
            yield return new object[] { -13194515, new byte[] { 0xed, 0xaa, 0x36, 0xff } };
            yield return new object[] { -2068145902, new byte[] { 0x12, 0x99, 0xba, 0x84 } };
            yield return new object[] { int.MinValue, new byte[] { 0x00, 0x00, 0x00, 0x80 } };
        }

        [Theory]
        [MemberData(nameof(FromIntTests_MemberDataSeed))]
        public void ToByteArray_FromIntTests(int i, byte[] expectedBytes)
        {
            BigInteger bi = new BigInteger(i);
            byte[] bytes = bi.ToByteArray();
            Assert.Equal(expectedBytes, bytes);
            BigInteger bi2 = new BigInteger(bytes);
            Assert.Equal(bi, bi2);
        }

        // long => signed little endian byte representation
        // the matrix of unsigned / big-endian is built from this.
        public static IEnumerable<object[]> FromLongTests_MemberDataSeed()
        {
            yield return new object[] { 0x100112233L, new byte[] { 0x33, 0x22, 0x11, 0x00, 0x01 } };
            yield return new object[] { 0x300112233L, new byte[] { 0x33, 0x22, 0x11, 0x00, 0x03 } };
            yield return new object[] { 0x8000112233L, new byte[] { 0x33, 0x22, 0x11, 0x00, 0x80, 0x00 } };
            yield return new object[] { 0x3cd00112233L, new byte[] { 0x33, 0x22, 0x11, 0x00, 0xcd, 0x03 } };
            yield return new object[] { 0xf92100112233L, new byte[] { 0x33, 0x22, 0x11, 0x00, 0x21, 0xf9, 0x00 } };
            yield return new object[] { 0x749aa00112233L, new byte[] { 0x33, 0x22, 0x11, 0x00, 0xaa, 0x49, 0x07 } };
            yield return new object[] { 0x80112200112233L, new byte[] { 0x33, 0x22, 0x11, 0x00, 0x22, 0x11, 0x80, 0x00 } };
            yield return new object[] { 0x7654321000112233L, new byte[] { 0x33, 0x22, 0x11, 0x00, 0x10, 0x32, 0x54, 0x76 } };
            yield return new object[] { long.MaxValue, new byte[] { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f } };
            yield return new object[] { -0x100112233L, new byte[] { 0xcd, 0xdd, 0xee, 0xff, 0xfe } };
            yield return new object[] { -0x8000112233L, new byte[] { 0xcd, 0xdd, 0xee, 0xff, 0x7f, 0xff } };
            yield return new object[] { -0x3cd00112233L, new byte[] { 0xcd, 0xdd, 0xee, 0xff, 0x32, 0xfc } };
            yield return new object[] { -0xf92100112233L, new byte[] { 0xcd, 0xdd, 0xee, 0xff, 0xde, 0x06, 0xff } };
            yield return new object[] { -0x749aa00112233L, new byte[] { 0xcd, 0xdd, 0xee, 0xff, 0x55, 0xb6, 0xf8 } };
            yield return new object[] { -0x80112200112233L, new byte[] { 0xcd, 0xdd, 0xee, 0xff, 0xdd, 0xee, 0x7f, 0xff } };
            yield return new object[] { -0x7654321000112233L, new byte[] { 0xcd, 0xdd, 0xee, 0xff, 0xef, 0xcd, 0xab, 0x89 } };
            yield return new object[] { long.MinValue, new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80 } };
            yield return new object[] { -0x100000000L, new byte[] { 0x00, 0x00, 0x00, 0x00, 0xff } };
            yield return new object[] { -0x300000000L, new byte[] { 0x00, 0x00, 0x00, 0x00, 0xfd } };
            yield return new object[] { -0x8000000000L, new byte[] { 0x00, 0x00, 0x00, 0x00, 0x80 } };
            yield return new object[] { -0xfe00000000L, new byte[] { 0x00, 0x00, 0x00, 0x00, 0x02, 0xff } };
            yield return new object[] { -0xff00000000L, new byte[] { 0x00, 0x00, 0x00, 0x00, 0x01, 0xff } };
            yield return new object[] { -0xfeff00000000L, new byte[] { 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0xff } };
            yield return new object[] { -0xffff00000000L, new byte[] { 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0xff } };
            yield return new object[] { -0xfeffff00000000L, new byte[] { 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0xff } };
            yield return new object[] { -0xffffff00000000L, new byte[] { 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0xff } };
        }

        [Theory]
        [MemberData(nameof(FromLongTests_MemberDataSeed))]
        public void ToByteArray_FromLongTests(long l, byte[] expectedBytes)
        {
            BigInteger bi = new BigInteger(l);
            byte[] bytes = bi.ToByteArray();
            Assert.Equal(expectedBytes, bytes);
            BigInteger bi2 = new BigInteger(bytes);
            Assert.Equal(bi, bi2);
        }

        // string => signed big endian byte representation
        // the matrix of unsigned / little-endian is built from this.
        public static IEnumerable<object[]> FromStringTests_MemberDataSeed()
        {
            yield return new object[] { "0", new byte[] { 0 } };
            yield return new object[] { "127", new byte[] { 0x7F } };
            yield return new object[] { "128", new byte[] { 0x00, 0x80 } };
            yield return new object[] { "33022", new byte[] { 0x00, 0x80, 0xFE } };
            yield return new object[] { "-32514", new byte[] { 0x80, 0xFE } };
            yield return new object[] { "-18374686475376656384", new byte[] { 0xff, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 } };
            yield return new object[] { "-18446744069414584320", new byte[] { 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 } };
            yield return new object[] { "12364093636075302621581796971036731159910277399901099375861080574506898189967563102391591201943540153492149033597384108294298661111799425262742266671957323486506857011293831406641316917374615487590434666756862524718179898130435474398176187353530443750986128762955255137790631223519091107256083370837751230886741470719554141609004126665185940207631783192780186624919167901565253877397154522582982120840364212333366972597473045167409184762879376102731157263235904816835054250885637739932914015117504485702897712016943555929710070193511128651378003670520306509417025958556935067307123832592173743652726898616663215056178200135054604939862749863002480827458494339065850786495122398411385446068167146399066035389699059499954747263948595492570959736940353095030589033792026029085438901525807032848103038290802923997216273712349905230080849822000411239032166420681788646737098078876258256753058713515144604794792139863186822520361187685685356628044518258989638970651859178568755065879235553215218267011024902142765549091793970027321040036512883269922219608216030987714165823965404563131018846411947569009598716649463301689241563037584803938711917477764863093448004918315253652379776337794243677630540042379049027", new byte[] { 0x00, 0xf0, 0x1d, 0x7b, 0xa2, 0xfd, 0x50, 0xf9, 0x50, 0x07, 0xce, 0x2a, 0xdf, 0xfb, 0x75, 0x70, 0x49, 0x5e, 0x5d, 0xf7, 0x1b, 0x0a, 0x76, 0x1f, 0xe7, 0xe3, 0xc7, 0x0a, 0xc7, 0xee, 0xca, 0x4e, 0xa2, 0xe6, 0xae, 0xe8, 0x1a, 0x2c, 0x3b, 0xa9, 0x12, 0x77, 0xb1, 0x47, 0x3c, 0xb5, 0xab, 0x32, 0x26, 0x18, 0x0d, 0x43, 0x51, 0x6b, 0x11, 0x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, 0xc0, 0xb6, 0xb7, 0xa8, 0x4a, 0x45, 0x6b, 0x98, 0x8f, 0x5b, 0xb8, 0x87, 0xf5, 0x64, 0xd1, 0x67, 0xd6, 0xdd, 0x76, 0x29, 0xf0, 0x77, 0x45, 0xfc, 0x27, 0xa4, 0x4a, 0xac, 0xaa, 0x1a, 0x90, 0xbe, 0xf8, 0x91, 0x50, 0x30, 0x6d, 0x73, 0x49, 0x3c, 0xab, 0xa4, 0xaf, 0x13, 0xa8, 0xdd, 0x0a, 0xf5, 0xc3, 0x34, 0x98, 0xb4, 0x7f, 0x0b, 0xb4, 0x63, 0x0f, 0xe3, 0x6e, 0xcc, 0xf8, 0x1d, 0xea, 0x34, 0x71, 0x8b, 0x06, 0xa5, 0x4f, 0x81, 0x12, 0xa9, 0xfd, 0xb8, 0x43 } };
            yield return new object[] { "-1500566381192798663042470094195709329888702572609999105286667163606153967917290138712408909178830809119008530236180974166477902167438722120032328817587204550920260039698718901932896233377293832747667996098291476468951412311792579258943277768740116090186686963715856483147820131067091322876669577393210366426487028489206389247040659375214497672166049911500383423054774768058254673393263777607084960848658501529675225559011863409579602879830226289200557853195239635009395081629845517655774307827048680743808664945667425540772434721298573889660136104342949269877806888346513652384111162522895043239369803590295673472600852282901495791087130835483730406094888840219829189061112151151001898468218215751327382511813082884657126285166508024920536549363279290734920537316417286759974845704859198588552892947264856714040130408613412473996518038227113385600353365608575770837750435553392852567153310861059766627383200108369176626042676222592985818614861742325706568951932845447397512345181981733524874445871904571909006536844113768514813826258460666950996534336842871477679076553532049073323784011247842133811527684859166963235747571306708361946012941167234981369225227878132777327321706825860067928644566542309248", new byte[] { 0xe2, 0xdb, 0xc3, 0xee, 0xdc, 0xd0, 0xa4, 0xdc, 0x41, 0xec, 0xf7, 0xf3, 0x53, 0x2f, 0xd9, 0x5e, 0xe5, 0xbf, 0x5e, 0xea, 0x84, 0xde, 0x00, 0x79, 0x48, 0xc6, 0xf4, 0xd0, 0x29, 0xd9, 0x41, 0xb3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0x5c, 0xfe, 0x54, 0xc1, 0x68, 0x40, 0x64, 0x99, 0x66, 0x2e, 0x18, 0x6b, 0xa6, 0x0a, 0xfe, 0x13, 0xec, 0x1e, 0x6f, 0xf3, 0xa0, 0xc9, 0x17, 0x9b, 0x6e, 0x92, 0xf2, 0x42, 0xfb, 0xbd, 0xfb, 0x11, 0x53, 0x7c, 0xeb, 0xd4, 0xd2, 0x16, 0xaf, 0x20, 0x5e, 0x42, 0x09, 0xea, 0x92, 0x0b, 0xab, 0x94, 0xaa, 0xaf, 0x23, 0xda, 0xda, 0xe3, 0x40, 0xc1, 0x5a, 0xab, 0xc2, 0xdd, 0x8d, 0x76, 0x62, 0xb3, 0xd4, 0x13, 0xac, 0x0c, 0x06, 0xa9, 0x98, 0x27, 0xa8, 0x34, 0x94, 0xd6, 0x85, 0x99, 0x24, 0x7e, 0x8a, 0xf1, 0xf8, 0x6d, 0xef, 0x66, 0xf0, 0xe6, 0xc9, 0x2c, 0x4d, 0x1f, 0x3e, 0xfa, 0x28, 0x80 } };
        }

        [Theory]
        [MemberData(nameof(FromStringTests_MemberDataSeed))]
        public void ToByteArray_FromStringTests(string str, byte[] expectedBytes)
        {
            BigInteger bi = BigInteger.Parse(str);
            byte[] bytes = bi.ToByteArray();

            // The expected data is big endian, the export is little endian.
            // Since this method already owns the exported data array reverse it instead of the input.
            Array.Reverse(bytes);
            Assert.Equal(expectedBytes, bytes);

            // Now put it back.
            Array.Reverse(bytes);

            BigInteger bi2 = new BigInteger(bytes);
            Assert.Equal(bi, bi2);
        }
    }
}
